#include <iostream>
#include <vector>

using std::cout;
using std::endl;
using std::vector;

template <typename  Container>
void display(const Container &con)
{
    for(auto &elem : con)
    {
        cout << elem << "  ";
    }
    cout << endl;
}


void test()
{
    vector<int> number = {1, 3, 5, 7, 9, 8, 6, 4, 2, 10};
    display(number);
    cout << "size() = " << number.size() << endl;
    cout << "capacity() = " << number.capacity() << endl;

    cout << endl << "在vector的尾部进行插入与删除" << endl;
    number.push_back(100);
    number.push_back(200);
    display(number);
    cout << "size() = " << number.size() << endl;
    cout << "capacity() = " << number.capacity() << endl;

    cout << endl << "在vector的任意位置插入元素" << endl;
    auto it = number.begin();
    ++it;
    ++it;
    cout << "*it = " << *it << endl;
    number.insert(it, 300);
    display(number);
    cout << "*it = " << *it << endl;
    cout << "size() = " << number.size() << endl;//13
    cout << "capacity() = " << number.capacity() << endl;//20

    cout << endl;
    it = number.begin();
    ++it;
    ++it;
    cout << "*it = " << *it << endl;
    //对于push_back，每次插入元素的个数是固定的，只要原始的空间
    //大小不是0，那么每次按照两倍的扩容肯定是够的，但是对于insert
    //插入而言，因为并不知道每次插入元素的个数是多少，所以不能
    //按照某个值的两倍进行扩容
    //capacity() = n, size() = m, 插入元素的个数t
    //1、t < n - m;就不会扩容
    //2、n - m < t < m;会按照2 * size()进行扩容
    //3、n - m < t < n;  n > t > m,会按照t + m进行扩容
    //4、n - m < t, t > n,会按照t + m进行扩容
    number.insert(it, 50, 55);
    display(number);
    cout << "*it = " << *it << endl;
    cout << "size() = " << number.size() << endl;
    cout << "capacity() = " << number.capacity() << endl;

    cout << endl;
    vector<int> vec = {12, 34, 56, 78};
    it = number.begin();
    ++it;
    ++it;
    cout << "*it = " << *it << endl;
    number.insert(it, vec.begin(), vec.end());
    display(number);
    cout << "*it = " << *it << endl;
    cout << "size() = " << number.size() << endl;
    cout << "capacity() = " << number.capacity() << endl;
}

int main(int argc, char *argv[])
{
    test();
    return 0;
}

